package com.czl.elm.dao.impl;

import com.czl.elm.domain.Search;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

@Repository
@Component
public class UsersMapperImpl {
    @Autowired
    private MongoTemplate mongoTemplate;


    public void addUsersAddress(String phoneName, Map<String, Object> msg,String adrName) {
        //long len=mongoTemplate.count(new Query(Criteria.where("_id").is(phoneName)),"address");
        //mongoTemplate.upsert(new Query(Criteria.where("_id").is(phoneName)), new Update().set("cname", msg), "address");

        Query query=new Query(Criteria.where("_id").is(phoneName));
        Map<String,Object> mgt =  mongoTemplate.findOne(query , Map.class,"address");
        if(mgt==null){
            mgt=new HashMap<>();
            mgt.put("_id",phoneName);
            mgt.put("default","null");
            Map<String,Object> address=new HashMap<>();
            mgt.put("address",address);
        }
        Map<String,Object> adr=(Map<String,Object>)mgt.get("address");
        System.out.println("adr:"+adr);
        msg.put("enable",1);
        if(adrName==null) adrName="address"+System.currentTimeMillis();
        adr.put(adrName,msg);
        mgt.put("address",adr);
        if(mgt.get("default").equals("null")){
            mgt.put("default",adrName);
        }
        System.out.println(mgt);
        mongoTemplate.save(mgt,"address");

    }

    public Map<String, Object> getUsersAddress(String phoneName) {
        Query query=new Query(Criteria.where("_id").is(phoneName));
        Map<String,Object> mgt =  mongoTemplate.findOne(query , Map.class,"address");
        return mgt;
    }

    public boolean delUsersAddress(String phoneName, String addressId) {
        Query query=new Query(Criteria.where("_id").is(phoneName));
        Map<String,Object> mgt =  mongoTemplate.findOne(query , Map.class,"address");
        Map<String,Object> adr=(Map<String,Object>)mgt.get("address");

        if(adr.containsKey(addressId))adr.remove(addressId);
        else return false;
        if(mgt.get("default").equals(addressId)){
            if(adr.size()>0){
                Iterator<String> iterator=adr.keySet().iterator();
                if(iterator.hasNext()){
                    mgt.put("default",iterator.next());
                }

            }else{
                mgt.put("default","null");
            }
        }
        mgt.put("address",adr);
        mongoTemplate.save(mgt,"address");
        return true;
    }

    public void setDefaultUsersAddress(String phoneName, String addressId) {
        Query query=new Query(Criteria.where("_id").is(phoneName));
        Map<String,Object> mgt =  mongoTemplate.findOne(query , Map.class,"address");
        mgt.put("default",addressId);
        mongoTemplate.save(mgt,"address");
    }

    public void setUsersAddress(String phoneName, String addressId,Map<String,Object> msg) {

        addUsersAddress(phoneName,msg,addressId);
    }

    public void setShopCart(String phoneName, String shopId, Map<String, Object> msg) {
        String name="phone_"+phoneName+"_shopId_"+shopId;
        msg.put("_id",name);
        mongoTemplate.save(msg,"shopCart");
    }

    public Map<String, Object> getShopCart(String phoneName, String shopId) {
        String name="phone_"+phoneName+"_shopId_"+shopId;
        Query query=new Query(Criteria.where("_id").is(name));
        Map<String,Object> mgt =  mongoTemplate.findOne(query , Map.class,"shopCart");
        return mgt;
    }

    public Object getUsersAddressById(String phoneName,String addressId) {
        Query query=new Query(Criteria.where("_id").is(phoneName));
        query.fields().include("address");
        Map<String,Object> mgt =  mongoTemplate.findOne(query , Map.class,"address");
        Map<String,Object> address=(Map<String,Object>)mgt.get("address");

        return address.get(addressId);
    }
}
